package com.xbts.screen.monitor.domain.request;

import com.alibaba.fastjson.JSON;
import com.xbts.screen.monitor.domain.constant.MonitorConstant;
import com.xbts.screen.monitor.domain.response.GetPublicKeyResponse;
import com.xbts.screen.monitor.domain.token.OauthConfigBaseInfo;
import com.xbts.screen.monitor.domain.util.HttpURLConnectionUtil;
import com.xbts.screen.monitor.domain.util.HttpsURLConnectionUtil;
import com.xbts.screen.monitor.domain.util.UrlUtil;

/**
 * 获取公钥
 */
public class GetPublicKeyRequest extends BaseRequest<GetPublicKeyResponse> {

    public GetPublicKeyRequest(OauthConfigBaseInfo oauthConfigBaseInfo){
        super(oauthConfigBaseInfo, MonitorConstant.HTTP_GET, oauthConfigBaseInfo.getHttpConfigInfo().getPrefixUrl() + MonitorConstant.GET_PUBLIC_KEY, false);
    }

    public GetPublicKeyResponse doAction(){
        //重置url信息，替换版本号
        this.setUrl(UrlUtil.dealVersion(this.getUrl()));
        //发送请求
        String res = isEnableHttpTest() ? HttpURLConnectionUtil.doGet(this) : HttpsURLConnectionUtil.doGet(this);
        //获取响应
        GetPublicKeyResponse getPublicKeyResponse = JSON.parseObject(res, GetPublicKeyResponse.class);

        return getPublicKeyResponse;
    }

    public boolean isEnableHttpTest(){
        return super.getOauthConfigBaseInfo().getHttpConfigInfo().isEnableHttpTest();
    }
}
